package cz.muni.fi.pv112.cranesimulator.gui.components.handlers;

import cz.muni.fi.pv112.cranesimulator.gui.components.ControlKeyTextField;
import cz.muni.fi.pv112.cranesimulator.utils.KeyCodeKeyTextTransformer;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Michal Ďuriš
 */
public class ControlKeysHandler {

    List<ControlKeyTextField> controlKeysList = new ArrayList<>();

    private ControlKeysHandler() {

    }

    public static ControlKeysHandler getInstance() {
        return ControlKeysHandlerHolder.INSTANCE;
    }

    private static class ControlKeysHandlerHolder {

        private static final ControlKeysHandler INSTANCE = new ControlKeysHandler();
    }

    public void addControlKeyItem(ControlKeyTextField controlKeyItem) {
        controlKeysList.add(controlKeyItem);
    }

    public void removeControlKeyItem(ControlKeyTextField controlKeyItem) {
        controlKeysList.remove(controlKeyItem);
    }

    public void loadAllControlKeys() {
        controlKeysList.stream().forEach(ControlKeyTextField::loadControlKey);
    }

    public void storeAllControlKeys() {
        controlKeysList.stream().forEach(ControlKeyTextField::storeControlKey);
    }

    public boolean isKeyAlreadyUsed(String key) {

        return controlKeysList
                .stream()
                .anyMatch((controlKeyItem) -> (KeyCodeKeyTextTransformer
                        .getKeyCode(controlKeyItem.getText())
                        == KeyCodeKeyTextTransformer.getKeyCode(key)));
    }

}
